package com.diaohaiwen.framework.mq.rocket.config;

import com.diaohaiwen.framework.mq.rocket.properties.ClientProperties;
import com.diaohaiwen.framework.mq.rocket.properties.ProducerProperties;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 刁海文
 * @since 2024/1/16 22:08
 */
@Configuration
public class ProducerConfig extends BaseRocketConfig {


    @Bean
    public DefaultMQProducer producer(ClientProperties config) throws MQClientException {

        ProducerProperties properties = config.getProducer();
        DefaultMQProducer producer = new DefaultMQProducer();
        initial(producer, config);

        producer.setUseTLS(producer.isUseTLS());

        producer.setAutoBatch(producer.getAutoBatch());

        producer.setProducerGroup(producer.getProducerGroup());

        producer.setCreateTopicKey(producer.getCreateTopicKey());

        producer.setSendMsgTimeout(properties.getSendMsgTimeout());

        producer.setCompressMsgBodyOverHowmuch(producer.getCompressMsgBodyOverHowmuch());

        producer.setRetryAnotherBrokerWhenNotStoreOK(properties.isRetryAnotherBrokerWhenNotStoreOK());

        producer.setMaxMessageSize(properties.getMaxMessageSize());

        producer.setDefaultTopicQueueNums(properties.getDefaultTopicQueueNums());

        producer.setRetryTimesWhenSendFailed(properties.getRetryTimesWhenSendFailed());

        producer.setSendMessageWithVIPChannel(producer.isSendMessageWithVIPChannel());

        producer.setNotAvailableDuration(producer.getNotAvailableDuration());

        producer.setLatencyMax(producer.getLatencyMax());

        producer.setSendLatencyFaultEnable(producer.isSendLatencyEnable());

        producer.setRetryTimesWhenSendAsyncFailed(properties.getRetryTimesWhenSendAsyncFailed());

        producer.setEnableBackpressureForAsyncMode(producer.isEnableBackpressureForAsyncMode());

        producer.setBackPressureForAsyncSendNum(producer.getBackPressureForAsyncSendNum());

        producer.setBackPressureForAsyncSendSize(producer.getBackPressureForAsyncSendSize());

        producer.start();
        return producer;

    }
}
